﻿namespace BM.Tools.WebControls.MForm.Controls.DefaultTemplates
{
    using System.Web.UI;
    using System.Web.UI.WebControls;

    using BM.Tools.WebControls.MForm.Controls.Helper;
    using BM.Tools.WebControls.MForm.Controls.Items;

    /// <summary>
    /// An <see cref="ITemplate" /> that instantiates a default label in the container control.    
    /// Used to generate default labels in <see cref="LeafItem" /> objects
    /// </summary>
    public class DefaultLabelTemplate : ITemplate
    {
        private const string LabelCssClass = "MForm-Lbl";

        /// <summary>
        /// Initializes a new instance of the <see cref="DefaultLabelTemplate"/> class. 
        /// </summary>
        /// <param name="owner">
        /// The owner.
        /// </param>
        public DefaultLabelTemplate(Leaf owner)
        {
            Owner = owner;
        }

        /// <summary>
        /// Gets Owner.
        /// </summary>
        /// <value>
        /// The owner.
        /// </value>
        public Leaf Owner
        {
            get;
            private set;
        }

        /// <summary>
        /// Creates a label with owner display name text in the container
        /// </summary>
        /// <param name="container">
        /// The container.
        /// </param>
        public void InstantiateIn(Control container)
        {
            container.Controls.Add(GenerateLabel());
        }
      
        private Control GenerateLabel()
        {
            Label label = new Label();
            PrepareLabel(label);
            return label;
        }

        private void PrepareLabel(Label label)
        {
            CssClasses cssClasses = new CssClasses(label, LabelCssClass);
            label.Text = Owner.DisplayName;
        }
    }
}